import Vue from 'vue'
import Vuex from 'vuex'
import persistedstate from "vuex-persistedstate"

Vue.use(Vuex)



export default new Vuex.Store({
  state: {
    searchHistoryList: [],
    token: ''
  },
  mutations: {
    addSearchHistoryList(state, value) {
      let flag = false

      state.searchHistoryList.forEach((item, index) => {
        if (item == value) {
          flag = true
          state.searchHistoryList.splice(index, 1)
        }
      })

      if (!flag && state.searchHistoryList.length >= 5) {
        state.searchHistoryList.splice(state.searchHistoryList.length - 1, 1)
      }

      state.searchHistoryList.unshift(value)
    },

    delSearchHistoryList(state) {
      state.searchHistoryList = []
    },

    token(state, token) {
      state.token = token
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [persistedstate()]
})

